package com.mesnac.gof.pattern.memento;
/**
 * 备忘录发起角色
 * @author chilei
 *
 */
public class Originator {
	//This is the state need save
	private int state;
	//一个管理者
	private Caretaker c = new Caretaker();
	
	public void setMemento(){
		Memento memento = (Memento)c.getM(); 
		state = memento.getState();
	}
	
	public void undo(){
		Memento memento = (Memento)c.getPrememento();
		if(memento!=null)
			state = memento.getState();
	}
	public void saveMemento(){
		c.addMemento(new Memento(state));
	}
	
	
	//创建一个备忘录
	public void createMemento(){
		c.saveMemento(new Memento(state));
	}
	
	private class Memento implements MementoIF{
		private int state;
		
		private Memento(int state){
			this.state = state;
		}

		public int getState() {
			return state;
		}

		public void setState(int state) {
			this.state = state;
		}
		
	}


	public int getState() {
		return state;
	}


	public void setState(int state) {
		this.state = state;
	}
	
	public static void main(String args[]){
		Originator a = new Originator();
		a.setState(6);
		a.saveMemento();
		a.setState(5);
		a.saveMemento();
		a.setState(4);
		a.saveMemento();
		a.setState(3);
		a.saveMemento();
		a.setState(2);
		a.saveMemento();
		a.setState(1);
		a.saveMemento();
		a.setState(0);
		System.out.println(a.state);
		a.undo();
		a.undo();
		a.undo();
		a.undo();
		a.undo();
		//a.undo();
		System.out.println(a.state);
	}
	
}
